package com.api.hg.scheld;

import com.alibaba.fastjson.JSONObject;
import com.api.hg.BaseUtle;
import com.api.hg.model.ZcglyException;
import com.api.hg.model.swException;
import com.api.hg.service.SwExceptionBaseService;
import com.api.hg.service.ZsbflExceptionBaseService;
import com.api.sw.duban.util.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

//com.api.hg.scheld.ZsbflExceptionConjob
//账实不符率 提醒
public class ZsbflExceptionConjob extends BaseCronJob {
    private final static LogTool log = new LogTool("/log/assetlog/luolf/ZsbflExceptionConjob",false);
    @Override
    public void execute() {
        //bengin conjob\
        log.writeLog(" begin--ZsbflExceptionConjob ");
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ZsbflExceptionBaseService ZsbflExceptionBaseService=new ZsbflExceptionBaseService();
        List<Object> list=new ArrayList<>();
        RecordSet re=new RecordSet();
        JSONObject json=null;
        ZcglyException ZcglyException=null;
        String SQL="select *  from uf_zsbfl_view";
        /*
            设置通知管理员
         */
       String zbz="";
       String ddw="";//多单位
        re.execute(SQL);
        while(re.next()){
            String id=Util.null2String(re.getString("id"));
            String subcompanyname=Util.null2String(re.getString("subcompanyname"));
            String swzcgly=Util.null2String(re.getString("swzcgly")).trim();
            double bfl=Util.getDoubleValue(re.getString("bfl"),0);
            if("".equals(swzcgly)){//设置异常数据
                ZcglyException=new ZcglyException();
                ZcglyException.setTxbm(id);
                ZcglyException.setTsnr(subcompanyname+"：未设置资产管理员");
                ZcglyException.setGnly("2");
                ZcglyException.setJctxsj(sim.format(new Date()));
                list.add(ZcglyException);
                continue;
            }
            if(bfl>=BaseUtle.txbz){//设置提醒
                json=new JSONObject();
                json.put("xgbm", id);//xgbm
                json.put("txr", 1);//txr 1
                json.put("tzr", swzcgly); //tzr
                String bz="<span class=\"wea-url\" style=\"color: red;\">"+subcompanyname+":帐时不符率超出了标准"+BaseUtle.txbz+"，实际不符率为"+bfl+"请尽快处理</span>";
                zbz=zbz+"<span class=\"wea-url\" style=\"color: red;\">"+subcompanyname+":帐时不符率超出了标准"+BaseUtle.txbz+"，实际不符率为"+bfl+"请尽快处理</span></br>";
                ddw=ddw+id+",";
                json.put("bz", bz); //bz
                settx(json);
            }

        }

        if(list.size()>0){
            log.writeLog("ZsbflExceptionConjob--->"+list.toString());
            ZsbflExceptionBaseService.getObjMapList(list,null);
        }
        ///spa/cube/index.html#/main/cube/search?customid=199&_key=b7ljd6
        if(!"".equals(zbz)){
            String tzr=BaseUtle.getRoleHrm(BaseUtle.sysadmin+"");
            ddw=ddw.substring(0,ddw.length()-1);
            if(!"".equals(tzr)){
                json=new JSONObject();
                json.put("xgbm", ddw);//xgbm
                json.put("txr", 1);//txr 1
                json.put("tzr", tzr); //tzr
                zbz=zbz+"<a title=\"\" href=\"javascript:void(0)\" onclick=\"javaScript:window.open('/spa/cube/index.html#/main/cube/search?customid="+BaseUtle.bflCustomid+"&_key=b7ljd6','_blank ');\" class=\"autoWrap\">点击此处查看详细数据</a>";
                json.put("bz", zbz);
                settx(json);
            }
        }
    }

    /*
        添加默认共享权限
     */
    public void settx(JSONObject json){
        new Thread(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
                log.writeLog("CreateTxWf"+json.toJSONString());
                try{

                    log.writeLog("json"+json.toJSONString());
                    //创建流程
                    User user=new User(Integer.parseInt(1+""));//creatorid  9914
                    //String SubCompanyname=BaseUtil.getSubcompayName(user.getUserSubCompany1()+"") ;

                    RequestInfo ri=new RequestInfo();
                    //添加主表数据
                    MainTableInfo mti = new MainTableInfo();
                    //读取创建人
                    ri.setCreatorid("1");//表单创建人id(申请人)
                    //流程ID
                    ri.setWorkflowid(BaseUtle.txwf);//根据问题类型查找对应的流程ID
                    //log.error("issubmit5--->"+issubmit);
                    //流程标题  Title+"-"+SubCompanyname+"-"+
                    String title="资产帐时不符率提醒"+"-"+sim.format(new Date());
                    ri.setDescription(title);
                    ri.setIsNextFlow("1");//提交至下一个节点
                    //设置主表数据
                    log.writeLog("begin");
                    //主表信息
                    ri.setMainTableInfo(mti);
                    Property[] p = new Property[0];

                    p = Arrays.copyOf(p, (p.length+1));
                    p[p.length-1] = new Property();
                    p[p.length-1].setName("xgbm");
                    p[p.length-1].setValue(Util.null2String(json.getString("xgbm")));
                    log.writeLog(Util.null2String(json.getString("xgbm")) );

                    p = Arrays.copyOf(p, (p.length+1));
                    p[p.length-1] = new Property();
                    p[p.length-1].setName("txr");
                    p[p.length-1].setValue(Util.null2String(json.getString("txr")));
                    log.writeLog(Util.null2String(json.getString("txr")));

                    p = Arrays.copyOf(p, (p.length+1));
                    p[p.length-1] = new Property();
                    p[p.length-1].setName("tzr");
                    p[p.length-1].setValue(Util.null2String(json.getString("tzr")));
                    log.writeLog(Util.null2String(json.getString("tzr")));

                    p = Arrays.copyOf(p, (p.length+1));
                    p[p.length-1] = new Property();
                    p[p.length-1].setName("bz");
                    p[p.length-1].setValue(Util.null2String(json.getString("bz")));
                    log.writeLog(Util.null2String(json.getString("bz")));

                    mti.setProperty(p);
                    RequestService service = new RequestService();
                    log.writeLog("end");
                    String requestid=service.createRequest(ri);
                    log.writeLog(json.toString()+"requestid"+requestid);
                }catch(Exception e){
                    e.printStackTrace();
                    log.writeLog("requestid异常"+e.getMessage());
                }
            }
        }).start();
    }


}
